﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.Admin;

public partial class Admin_AdminManageMent : System.Web.UI.Page
{
    /// <summary>
    /// 台后用户管理
    /// </summary>

    private static string AID = "";
    private Boolean isName(string strname)
    {
        DataTable dt_admin = AdminServices.selectAllV_admin(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex)[0];
        for (int i = 0; i < dt_admin.Rows.Count; i++)
        {
            if (dt_admin.Rows[i]["AName"].ToString().Trim() == strname.ToString().Trim())
            {
                return false;
            }
        }
        return true;
    }
    //用于刷新的方法
    private void bindGrid()
    {
        try
        {
            AspNetPager1.RecordCount = Convert.ToInt32(AdminServices.selectAllV_admin(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex)[1].Rows[0][0].ToString().Trim());
            GVAdmin.DataSource = AdminServices.selectAllV_admin(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex)[0];
            GVAdmin.DataBind();
            update.Visible = false;
            lblMessages.Visible = false;
           
        }
        catch (Exception ex)
        {
            ClientScriptManager csm = this.ClientScript;
            csm.RegisterStartupScript(this.GetType(), "admin", "alter('" + ex.Message.ToString().Trim() + "')", true);
        }
    }

    //加载的时候执行
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"].ToString().Trim() != "1")
        {
            Response.Redirect("Login.aspx");
        }
        bntInsert.Visible = false;
        if (!IsPostBack)
        {
            bindGrid();
        }
    }

    //分页控件的刷新方法
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindGrid();
    }

    //点修改的时候
   private static  string  Name = "";
    protected void GVAdmin_SelectedIndexChanged(object sender, EventArgs e)
    {
        update.Visible = true;
        bntInsert.Visible = false;
        btnUpdate.Visible = true;
        try
        {
            ddlGL.DataSource = AdminServices.SP_select("AdminLevel", "ALID,ALNAame");
            ddlGL.DataTextField = "ALNAame";
            ddlGL.DataValueField = "ALID";
            ddlGL.DataBind();
            Name = GVAdmin.SelectedRow.Cells[2].Text.ToString().Trim();
            txtName.Text = GVAdmin.SelectedRow.Cells[2].Text.ToString().Trim();
            txtPwd.Text = GVAdmin.SelectedRow.Cells[3].Text.ToString().Trim();
            AID = GVAdmin.SelectedRow.Cells[0].Text.ToString().Trim();
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
    protected void GVAdmin_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            lblMessages.Visible = false;
            if (AdminServices.SP_delete("Admin", "AID=" + GVAdmin.Rows[e.RowIndex].Cells[0].Text.ToString().Trim()))
            {
                lblMessages.Visible = true;
                lblMessages.Text = "删除成功";
                bindGrid();
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "删除失败";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
    //修改
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (Name == txtName.Text.ToString())
            {
                string strMd5UserPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.ToString().Trim(), "md5");
                string strSql = "update Admin set  ALID='" + Convert.ToInt32(ddlGL.SelectedItem.Value.ToString().Trim())
                    + "',AName='" + txtName.Text.ToString().Trim() + "',APwd='" + strMd5UserPwd + "'  where  AID=" + Convert.ToInt32(AID.ToString().Trim());
                if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
                { 
                    bindGrid();
                    lblMessages.Visible = true;
                    lblMessages.Text = "修改成功";
                  
                    update.Visible = false;
                }
                else
                {
                    lblMessages.Visible = true;
                    lblMessages.Text = "修改失败";
                }
            }
            else
            {
                if (isName(txtName.Text.ToString().Trim()))
                {
                    string strMd5UserPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.ToString().Trim(), "md5");
                    string strSql = "update Admin set  ALID='" + Convert.ToInt32(ddlGL.SelectedItem.Value.ToString().Trim())
                        + "',AName='" + txtName.Text.ToString().Trim() + "',APwd='" + strMd5UserPwd + "'  where  AID=" + Convert.ToInt32(AID.ToString().Trim());
                    if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
                    { 
                        bindGrid();
                        lblMessages.Visible = true;
                        lblMessages.Text = "修改成功";
                        update.Visible = false;
                    }
                    else
                    {
                        lblMessages.Visible = true;
                        lblMessages.Text = "修改失败";
                    }
                }
                else
                {
                    lblMessages.Visible = true;
                    lblMessages.Text = "用户名已被占用，请您使用其它的用户名";
                }
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //添加新用户时
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        try
        {
            txtName.Text = "";
            txtPwd.Text = "";
            update.Visible = true;
            btnUpdate.Visible = false;
            bntInsert.Visible = true;

            ddlGL.DataSource = AdminServices.SP_select("AdminLevel", "ALID,ALNAame");
            ddlGL.DataTextField = "ALNAame";
            ddlGL.DataValueField = "ALID";
            ddlGL.DataBind();
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }

    //添加
    protected void bntInsert_Click(object sender, EventArgs e)
    {
        try
        {

            if (isName(txtName.Text.ToString().Trim()))
            {
                string strMd5UserPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.ToString().Trim(), "md5");
                string strSql = "insert into  Admin(ALID,AName,APwd)values(" + ddlGL.SelectedItem.Value.ToString().Trim() +
                    ",'" + txtName.Text.ToString().Trim() + "','" + strMd5UserPwd + "')";
                if (AdminServices.SP_sqlStr_Update_Delete_insert(strSql))
                {
                    lblMessages.Visible = true;
                    lblMessages.Text = "添加成功";
                    bindGrid();
                    update.Visible = false;
                }
                else
                {
                    lblMessages.Visible = true;
                    lblMessages.Text = "添加失败";
                }
            }
            else
            {
                lblMessages.Visible = true;
                lblMessages.Text = "用户名已被占用，请您使用其它的用户名";
            }
        }
        catch (Exception ex)
        {
            lblMessages.Visible = true;
            lblMessages.Text = ex.Message.ToString().Trim();
        }
    }
}
